<?php
# !!!!!!!!!! DON'T MAKE ANY CHANGE IF YOU DON'T KNOW WHAT YOU'RE DOING !!!!!!!!!!
// --------------------------------------------------------------------------------------------------------------
// File Name              :	mybasket.php
// Last Modification Time	:	2008-02-01 24:00
// Last Modified by       :	turker (turker.biz@gmail.com)
// list of modifications  :
//
//	- 2008-02-01 turker
//		* 24:00 file created.
//
// --------------------------------------------------------------------------------------------------------------
if (eregi("mybasket.php",$_SERVER['PHP_SELF'])) {
	header ("Location:../index.php");
	exit;
}

if (!isLogged()) {
	header ("Location:$SITE_URL/");
	exit;
}

$user_id=getActiveUser();
?>
<!--div:icerik -->
    <div id="icerik">
      <p>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
  $("#makeorder").click(function() {
    if(confirm("<?php echo $_LANG['basket']['msg1']; ?>")) {
      if (!siteSEO) top.location=siteURL+'index.php?page=makeorder';
      else top.location=siteURL+'makeorder';
    }
  });
});
</script>
<?php
# sepeti güncelle
if (!empty($_POST)) { # sepeti güncelle
  $update=@$_POST['update'];
  foreach ($update as $bid=>$val) {
    $val=(int)$val;
    $mysql->escape($val);
    $mysql->query("update $BASKET_TABLE set piece='$val' where id='$bid' and user_id='$user_id'");
  }

  $del=@$_POST['del'];
  foreach ($del as $val) {
    $mysql->escape($val);
    $mysql->query("delete from $BASKET_TABLE where id='$val'");
  }
}

$user_id=getActiveUser();
# sepetteki urunleri listele
//id, user_id, item_id, price_id, exchange_id, piece, date, ordered
//id, name, exchange
//id, item_id, exchange_id, price, info, campaign, campaign_price, active, vat
//id, item, summary, info, name, home, new, lang, show_price, xml_code, image
//RESULT : bid,piece,order_date,ename,exchange,price,campaign,campaign_price,vat,item
$active="and $ITEM_PRICES_TABLE.active='1'";
if (isAdmin()) $active='';
$sql="select $BASKET_TABLE.id as bid,$BASKET_TABLE.date as bdate,$BASKET_TABLE.piece,$EXCHANGES_TABLE.name as ename,$EXCHANGES_TABLE.exchange,$ITEMS_TABLE.id,";
$sql.="$ITEM_PRICES_TABLE.price,$ITEM_PRICES_TABLE.campaign,$ITEM_PRICES_TABLE.info,$ITEM_PRICES_TABLE.campaign_price,$ITEM_PRICES_TABLE.vat,$ITEMS_TABLE.item,$ITEMS_TABLE.name from ";
$sql.="$BASKET_TABLE,$ITEM_PRICES_TABLE,$EXCHANGES_TABLE,$ITEMS_TABLE where $BASKET_TABLE.user_id='$user_id' and $BASKET_TABLE.price_id=$ITEM_PRICES_TABLE.id and ";
$sql.="$BASKET_TABLE.exchange_id=$EXCHANGES_TABLE.id and $ITEMS_TABLE.lang='$SITE_DEF_LANG' $active  and $BASKET_TABLE.ordered='0' and ";
$sql.="$ITEM_PRICES_TABLE.item_id=$ITEMS_TABLE.id and $ITEM_PRICES_TABLE.exchange_id=$EXCHANGES_TABLE.id order by $BASKET_TABLE.date desc";

$q=$mysql->query($sql);
if ($mysql->numRows($q)>0) {
  $global_total=0;
  echo '<form id="basketForm" action="'.$SITE_URL.'/index.php?page=mybasket" method="post">';
  echo '<table cellpadding="2" cellspacing="0" width="100%">';
  echo '<tr><th align="center">'.$_LANG['general']['del'].'</th><th align="center">'.$_LANG['general']['date'].'</th>'.
       '<th>'.$_LANG['basket']['item'].'</th><th align="center">'.$_LANG['basket']['piece'].'</th>'.
       '<th align="right">'.$_LANG['basket']['upiece'].'</th><th align="right">'.$_LANG['basket']['vat'].'</th>'.
       '<th align="right">'.$_LANG['basket']['total'].'</th></tr>';
  while ($read=$mysql->fetch($q)) {
  	$basket_id=$read['bid'];
  	$piece=$read['piece'];
  	$date=$read['bdate']; #*
  	$ename=$prep->html($read['ename']);
  	$exchange=$read['exchange'];
  	$price=$read['price']; #*
  	$campaign=$read['campaign']; #*
  	$campaign_price=$read['campaign_price']; #*
  	$vat=$read['vat'];
  	$item=$prep->html($read['item']); #*
  	$item_id=$read['id']; #*
  	$name=$read['name']; #*
  	$price_info=$prep->html($read['info']); #**

  	$date=date('d/m/y H:i',$date);
  	if ($campaign_price>0&&$campaign) $price=$campaign_price;
    $item_link='<a href="index.php?page=item&id='.$item_id.'">'.$item.' '.$price_info.'</a>';
    if ($SITE_SEO) $item_link='<a href="'.$SITE_URL.'/item/'.$name.'">'.$item.' '.$price_info.'</a>';

    $price=round($price*$exchange,2);
    $vat=round($price*$piece*$vat/100,2);
    $total=round($price*$piece+$vat,2);
    $global_total+=$total;

  	echo <<<html
  	<tr id="$basket_id">
      <td align="center"><input type="checkbox" name="del[]" id="del_$basket_id" value="$basket_id" /></td>
      <td align="center">$date</td>
      <td>$item_link</td>
      <td align="center"><input type="text" size="2" name="update[$basket_id]" id="update_$basket_id" value="$piece" /></td>
      <td align="right">$price YTL</td>
      <td align="right">$vat YTL</td>
      <td align="right">$total YTL</td>
  	</tr>
html;
  }
  echo '<tr><td colspan="6" align="right">'.$_LANG['basket']['gtotal'].'</td><td align="right"><span id="global_total">'.$global_total.'</span> YTL</td></tr>';
  echo '<tr><td colspan="6" align="right"><input type="submit" value=" '.$_LANG['basket']['ubasket'].' " id="update" /></td>';
  echo '<td align="left">&nbsp;<input type="button" value=" '.$_LANG['basket']['morder'].' " id="makeorder" /></td></tr>';
  echo '</table></form>';
}
else echo $_LANG['basket']['error2'];
?>
      </p>
    </div>
<!--//div:icerik -->